<?php

class QuestionnaireController extends Zend_Controller_Action
{

    protected $_authNamespace;

    public function init()
    {
        /* Initialize action controller here */
        $this->_authNamespace = new Zend_Session_Namespace('Zend_Auth');
    }

    public function indexAction()
    {
        if($this->_authNamespace->userRole == 'admin') {
            $questionnaire = new Application_Model_QuestionnaireMapper();
            $this->view->entries = $questionnaire->fetchAll();
        } else {
            return $this->_helper->redirector->gotoUrl('/auth/login');
        }
    }

    public function addQuestionnaireAction()
    {
        if($this->_authNamespace->userRole == 'admin') {
            $request = $this->getRequest();
            $form    = new Application_Form_Questionnaire();
     
            if ($this->getRequest()->isPost()) {
                if ($form->isValid($request->getPost())) {
                    $questionnaire = new Application_Model_Questionnaire($form->getValues());
                    $questionnaire->setName($form->getValue('name'));
                    $questionnaire->setFailsNumber($form->getValue('fails_number'));
                    $mapper  = new Application_Model_QuestionnaireMapper();
                    $mapper->save($questionnaire);
                    return $this->_helper->redirector('index');
                }
            }
     
            $this->view->form = $form;
        } else {
            return $this->_helper->redirector('index');
        }
    }

    public function editQuestionnaireAction()
    {
        if($this->_authNamespace->userRole == 'admin') {
            $questionnaire_id = $this->_getParam("questionnaire_id");
            $request = $this->getRequest();
            $form    = new Application_Form_Questionnaire();
            $this->view->questionnaire_id = $questionnaire_id;

            if ($this->getRequest()->isPost()) {
                if ($form->isValid($request->getPost())) {
                    $data = array("id" => (int)$questionnaire_id);
                           
                    $questionnaire = new Application_Model_Questionnaire($data);
                    $mapper  = new Application_Model_QuestionnaireMapper();
                    $mapper->find($questionnaire_id, $questionnaire);
                    $questionnaire->setName($form->getValue('name'));
                    $questionnaire->setFailsNumber($form->getValue('fails_number'));
                    $mapper->save($questionnaire);
                    return $this->_helper->redirector('index');
                }
            } else {
                $data = array("id" => (int)$questionnaire_id);
                           
                $questionnaire = new Application_Model_Questionnaire($data);

                $mapper  = new Application_Model_QuestionnaireMapper();
                $mapper->find($questionnaire_id, $questionnaire);
                $form_data = array("id" => $questionnaire->getId(), "name" => $questionnaire->getName(), "fails_number" => $questionnaire->getFailsNumber());
                $form->populate($form_data);

                $question = new Application_Model_QuestionMapper();
                
                $this->view->questions = $question->fetchByQuestionnaire((int) $questionnaire_id);
            }
     
            
            $this->view->form = $form;
        } else {
            return $this->_helper->redirector('index');
        }
    }

    public function deleteAction()

    {
        if($this->_authNamespace->userRole == 'admin') {
            $questionnaire_id = $this->_getParam("questionnaire_id");
            $this->view->idValue = $questionnaire_id;
            if ($questionnaire_id != null) {
                $data = array("id" => (int)$questionnaire_id);
                           
                $questionnaire = new Application_Model_Questionnaire($data);
                $mapper  = new Application_Model_QuestionnaireMapper();
                $mapper->delete($questionnaire);

                return $this->_helper->redirector('index');
            }

        } else {
            $this->view->message = "You don't have permissions to do this!";
            return $this->_helper->redirector('index');
        }
    }



}



